# Add simple GUI for iOS security intrusion

The AppSealing library within your App is automatically activated when right after App has launched. If AppSealing library has detected any abnormal environment (such as jailbroken-device, executable has decrypted or debugger has attached) it will close the app after 20 seconds irrespectively of user action, so the app should notify the detection result to user and show some proper message box for user can recognize there's some invalid environment in his/her device.

If you want to show that dialog box in your app, you can easily do that inserting small chunk of code into "ViewController.swift" file. (ViewController.mm in case of Objective-C based project)

# Show UIAlertController window in your app

First, open your Xcode project and open ViewController.swift" file.

After you opened the swift file put following code into that (If ViewController.swift file has already included 'viewDidAppear' method just insert the body of following code below 'super.viewDidAppear( animated );' line.)

Simple UI code into ‘ViewController.swift’ for Swift project

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear( animated );

    let inst: AppSealingInterface = AppSealingInterface();
    let tamper: Int32 = inst._IsAbnormalEnvironmentDetected();
    if ( tamper > 0 )
    {
        var msg = "Abnormal Environment Detected !!";
        if ( tamper & DETECTED_JAILBROKEN ) > 0
{ msg += "\n - Jailbroken"; }
        if ( tamper & DETECTED_DRM_DECRYPTED ) > 0
{ msg += "\n - Executable is not encrypted"; }
        if ( tamper & DETECTED_DEBUG_ATTACHED ) > 0
{ msg += "\n - App is debugged"; }
if ( tamper & ( DETECTED_HASH_INFO_CORRUPTED | DETECTED_HASH_MODIFIED )) > 0
{ msg += "\n - App integrity corrupted"; }
    if ( tamper & ( DETECTED_CODESIGN_CORRUPTED | DETECTED_EXECUTABLE_CORRUPTED )) > 0
{ msg += "\n - App executable has corrupted"; }
    if ( tamper & DETECTED_CERTIFICATE_CHANGED ) > 0
{ msg += "\n - App has re-signed"; }
        let alertController = UIAlertController(title: "AppSealing",
 message: msg, preferredStyle: .alert );
        alertController.addAction(UIAlertAction(title: "Confirm", style: .default,
                                handler: { (action:UIAlertAction!) -> Void in exit(0); } ));
        self.present(alertController, animated: true, completion: nil);
    }
}

Sample UI code above is also included in "AppsealingiOS.mm" file so you can copy & paste int that file.

If your project is Objective-C based then you can use following code to show simple UI.

Simple UI code into ‘ViewController.mm (opens new window)’ for Objective-C project

#include "AppsealingiOS.h"

- (void)viewDidAppear:(BOOL)animated
{
   [super viewDidAppear:animated];

   int tamper = ObjC_IsAbnormalEnvironmentDetected();
if ( tamper > 0 )
   {
       NSString* msg = @"Abnormal Environment Detected !!";
       if (( tamper & DETECTED_JAILBROKEN ) > 0 )
           msg = [msg stringByAppendingString:@"\n - Jailbroken"];
       if (( tamper & DETECTED_DRM_DECRYPTED ) > 0 )
           msg = [msg stringByAppendingString:@"\n - Executable is not encrypted"];
       if (( tamper & DETECTED_DEBUG_ATTACHED ) > 0 )
           msg = [msg stringByAppendingString:@"\n - App is debugged"];
if ( tamper & ( DETECTED_HASH_INFO_CORRUPTED | DETECTED_HASH_MODIFIED )) > 0
   msg = [msg stringByAppendingString:@"\n - App integrity corrupted"];
if ( tamper & ( DETECTED_CODESIGN_CORRUPTED | DETECTED_EXECUTABLE_CORRUPTED )) > 0
   msg = [msg stringByAppendingString:@"\n - App executable has corrupted"];
if ( tamper & DETECTED_CERTIFICATE_CHANGED ) > 0
   msg = [msg stringByAppendingString:@"\n - App has re-signed"];
       
       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AppSealing"
                                      message:msg
                                      preferredStyle:UIAlertControllerStyleAlert];
       UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"Confirm"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * _Nonnull action) { exit(0); }];
       [alert addAction:confirm];
       [self presentViewController:alert animated:YES completion:nil];
   }
}

Your app will show simple alert box like below when you run your app on abnormal device such as jailbroken or debug your app using Xcode or gdb. In such situation irrespective of user action the app will exit after 20 seconds automatically.

If your project is flutter based, the ViewController.swift file is not created, so you can create the GUI by adding the following modified code to AppDelegate.swift file

Simple UI code into ‘AppDelegate.swift’ for flutter project

func application(_ application: UIApplication, didFinishLaunchingWithOptions
     launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
    // Override point for customization after application launch.

    let inst: AppSealingInterface = AppSealingInterface();
    let tamper: Int32 = inst._IsAbnormalEnvironmentDetected();
    if ( tamper > 0 )
    {
        var msg = "Abnormal Environment Detected !!";
        if ( tamper & DETECTED_JAILBROKEN ) > 0
{ msg += "\n - Jailbroken"; }
        if ( tamper & DETECTED_DRM_DECRYPTED ) > 0
{ msg += "\n - Executable is not encrypted"; }
        if ( tamper & DETECTED_DEBUG_ATTACHED ) > 0
{ msg += "\n - App is debugged"; }
if ( tamper & ( DETECTED_HASH_INFO_CORRUPTED | DETECTED_HASH_MODIFIED )) > 0
{ msg += "\n - App integrity corrupted"; }
    if ( tamper & ( DETECTED_CODESIGN_CORRUPTED | DETECTED_EXECUTABLE_CORRUPTED )) > 0
{ msg += "\n - App executable has corrupted"; }
    if ( tamper & DETECTED_CERTIFICATE_CHANGED ) > 0
{ msg += "\n - App has re-signed"; }
        let alertController = UIAlertController(title: "AppSealing",
 message: msg, preferredStyle: .alert );
        alertController.addAction(UIAlertAction(title: "Confirm", style: .default,
                                handler: { (action:UIAlertAction!) -> Void in
#if !DEBUG   // Debug mode does not kill app even if security threat has found
     exit(0);
#endif
} ));
		// show alert
let alertWindow = UIWindow( frame: UIScreen.main.bounds )
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present( alertController,
animated: true, completion: nil )	
}
return true;
}
Last Updated: 11/19/2024, 6:55:37 AM